package com.dong.web.util;

import java.lang.reflect.Method;
import java.util.Map;

import com.dong.web.whold.Variables;

public final class ClassUtil {

	public static Method getMethod(Map<String, Method> actionMethods,
			Method[] methods, String name) {
		if (actionMethods.containsKey(name)) {
			return actionMethods.get(name);
		} else {
			for (Method method : methods) {
				if (method.getName().equals(name)) {
					return method;
				}
			}
		}
		return null;
	}

	public static String getPath() {
		try {
			if (Variables.CLASSES_PATH == null) {
				String url = new ClassPath().getPath();
				if (url.startsWith("/")) {
					url = url.substring(1);
				}
				if (url.indexOf(Variables.WEB_INF) != -1) {
					url = url.substring(0, url.indexOf(Variables.WEB_INF) + 1);
				} else if (url.indexOf(Variables.WEB_INF_LIB) != -1) {
					url = url.substring(0,
							url.indexOf(Variables.WEB_INF_LIB) + 1);
				}
				url = (url + Variables.WEB_INF + "/").replace("//", "/");
				System.out.println(String.format(
						"[%s] DEBUG [ClassUtil:getPath] - classes:%s",
						new PrintUtil().getDate(), url));
				Variables.CLASSES_PATH = url;
			}
			return Variables.CLASSES_PATH;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String... strings) {
		getPath();
	}

	public static String getProjectPath() {
		String url = getPath();
		url = url.substring(0, url.length() - Variables.WEB_INF_LEN);
		return url;
	}

}
